package com.huadi.ssm.controller;


import com.huadi.ssm.page.Page;
import com.huadi.ssm.po.Cost;
import com.huadi.ssm.po.Project;
import com.huadi.ssm.po.Researcher;
import com.huadi.ssm.service.CostService;
import com.huadi.ssm.service.ProjectService;
import com.huadi.ssm.util.StringUtil;
import net.sf.json.JSONArray;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 科研经费管理
 *
 */
@RequestMapping("/cost")
@Controller
public class CostController {

    @Autowired
    private CostService costService;
    @Autowired
    private ProjectService projectService;
    /**
     * 科研经费列表页
     * @param model
     * @return
     */
    @RequestMapping(value="/list",method= RequestMethod.GET)
    public ModelAndView list(ModelAndView model, HttpServletRequest request){
        model.setViewName("cost/cost_list");
        Object attribute = request.getSession().getAttribute("userType");
        if("2".equals(attribute.toString())){
            //说明是科研工作人员
            Researcher loginedResearcher = (Researcher)request.getSession().getAttribute("user");
            Long id=loginedResearcher.getId();
            String researchid=id.toString();
            List<Project> projectList = projectService.findByResearchid(researchid);
            model.addObject("projectList",projectList );
            model.addObject("projectListJson", JSONArray.fromObject(projectList));
            return model;
        }
        else{
            List<Project> projectList = projectService.findAll();
            model.addObject("projectList",projectList );
            model.addObject("projectListJson", JSONArray.fromObject(projectList));
            return model;
        }
    }

    /**
     * 获取科研经费列表
     * @param page
     * @return
     */
    @RequestMapping(value="/get_list",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getList(
            @RequestParam(value="projectid",required=false) Long projectid,
            HttpServletRequest request,
            Page page
    ){
        Map<String, Object> ret = new HashMap<String, Object>();
        Map<String, Object> queryMap = new HashMap<String, Object>();
        Object attribute = request.getSession().getAttribute("userType");
        if("2".equals(attribute.toString())){
            //说明是科研工作人员
            Researcher loginedResearcher = (Researcher)request.getSession().getAttribute("user");
            queryMap.put("researchid", loginedResearcher.getId());
        }
        if(projectid != null){
            queryMap.put("projectid", projectid);
        }
        queryMap.put("offset", page.getOffset());
        queryMap.put("pageSize", page.getRows());
        ret.put("rows", costService.findList(queryMap));
        ret.put("total", costService.getTotal(queryMap));
        return ret;
    }

    /**
     * 编辑科研经费信息
     * @param cost
     * @return
     */
    @RequestMapping(value="/edit",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, String> edit(Cost cost, HttpServletRequest request){
        Map<String, String> ret = new HashMap<String, String>();
        if(StringUtils.isEmpty(cost.getBudget())){
            ret.put("type", "error");
            ret.put("msg", "科研经费预算不能为空！");
            return ret;
        }
        Object attribute = request.getSession().getAttribute("userType");
        if("2".equals(attribute.toString())){
            //说明是科研工作人员
            if(costService.editForRearcher(cost) <= 0){
                ret.put("type", "error");
                ret.put("msg", "科研经费信息修改失败！");
                return ret;
            }
            ret.put("type", "success");
            ret.put("msg", "科研经费信息修改成功！");
            return ret;

        }
        else{
            if(costService.edit(cost) <= 0){
                ret.put("type", "error");
                ret.put("msg", "科研经费信息修改失败！");
                return ret;
            }
            ret.put("type", "success");
            ret.put("msg", "科研经费信息修改成功！");
            return ret;

        }

    }


    /**
     * 添加科研经费信息
     * @param cost
     * @return
     */
    @RequestMapping(value="/add",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, String> add(Cost cost,HttpServletRequest request){
        Map<String, String> ret = new HashMap<String, String>();
        if(StringUtils.isEmpty(cost.getBudget())){
            ret.put("type", "error");
            ret.put("msg", "科研经费不能为空！");
            return ret;
        }
        try {
            if(costService.add(cost) <= 0){
                ret.put("type", "error");
                ret.put("msg", "科研经费添加失败！");
                return ret;
            }
        }catch (Exception e){
            ret.put("type", "error");
            ret.put("msg", "该所属项目下已存在经费信息，如需修改，请选择修改按钮！");
            return ret;
        }
        ret.put("type", "success");
        ret.put("msg", "科研经费添加成功！");
        return ret;
    }

    /**
     * 删除科研经费信息
     * @param ids
     * @return
     */
    @RequestMapping(value="/delete",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, String> delete(
            @RequestParam(value="ids[]",required=true) Long[] ids
    ){
        Map<String, String> ret = new HashMap<String, String>();
        if(ids == null || ids.length == 0){
            ret.put("type", "error");
            ret.put("msg", "请选择要删除的数据！");
            return ret;
        }
        try {
            if(costService.delete(StringUtil.joinString(Arrays.asList(ids), ",")) <= 0){
                ret.put("type", "error");
                ret.put("msg", "删除失败！");
                return ret;
            }
        } catch (Exception e) {
            // TODO: handle exception
            ret.put("type", "error");
            ret.put("msg", "该科研经费下存在其他信息，请勿冲动！");
            return ret;
        }
        ret.put("type", "success");
        ret.put("msg", "该科研经费删除成功！");
        return ret;
    }
}
